package cc.mrbird.febs.common.utils;

import java.util.Calendar;

public class CalendarUtil {

    /**
     * @param yield    转转的类型 1为月 2 为日
     * @param calendar 转换的日期
     * @return 日期
     */
    public static Calendar setMax(int yield, Calendar calendar) {
        if (Calendar.YEAR == yield) {
            calendar.set(Calendar.MONTH, calendar.getActualMaximum(Calendar.MONTH));
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            setMaxTime(calendar);
        } else if (Calendar.MONTH == yield) {
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            setMaxTime(calendar);
        } else if (Calendar.DAY_OF_MONTH == yield) {
            setMaxTime(calendar);
        } else {
            throw new RuntimeException("只能转换 月 或 日");
        }
        return calendar;
    }

    /**
     * @param yield    转转的类型 1 为年 2 为月
     * @param calendar 转换的日期
     * @return 日期
     */
    public static Calendar setMin(int yield, Calendar calendar) {
        if (Calendar.YEAR == yield) {
            calendar.set(Calendar.MONTH, calendar.getActualMinimum(Calendar.MONTH));
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
            setMinTime(calendar);
        } else if (Calendar.MONTH == yield) {
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
            setMinTime(calendar);
        } else if (Calendar.DAY_OF_MONTH == yield) {
            setMinTime(calendar);
        } else {
            throw new RuntimeException("只能转换 月 ,日 或当天");
        }
        return calendar;
    }


    private static void setMinTime(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
    }

    private static void setMaxTime(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
        calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
        calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
        calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));
    }
}
